package com.lj.cloudDisk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.cloudDisk.mapper.FileMapper;
import com.lj.cloudDisk.pojo.File;
import com.lj.cloudDisk.pojo.TreeNode;
import com.lj.cloudDisk.pojo.UserDir;
import com.lj.cloudDisk.service.FileService;
import com.lj.cloudDisk.service.UserDirService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FileServiceImpl extends ServiceImpl<FileMapper, File>  implements FileService {

    @Autowired
    FileMapper fileMapper;

    @Autowired
    UserDirService userDirService;
    /**
     * 获取当前目录的文件
     * @param userId 用户id
     * @param filePath 文件路径
     * @return
     */
    @Override
    public List<File> getCurDirFiles(String userId, String filePath) {
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(File::getMemId,userId);
        queryWrapper.eq(File::getFDir,filePath);
        List<File> files = fileMapper.selectList(queryWrapper);
        return files;
    }

    /**
     * 获取搜索文件
     * @param memId 用户id
     * @param content 搜索内容
     * @return
     */
    @Override
    public Map<String,Object> findFile(String memId, String content) {
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper();

        //获取文件
        queryWrapper.eq(File::getMemId,memId);
        queryWrapper.like(File::getName,content);
        List<File> files = fileMapper.selectList(queryWrapper);

        //获取文件夹
        UserDir userDir = userDirService.getUserDir(memId);
        TreeNode treeNode = JSON.parseObject(userDir.getMemDir(), new TypeReference<TreeNode>() {
        });
        List treeList=new ArrayList();
        findTreeNode(treeNode,content,treeList);

        Map<String, Object> map = new HashMap<>();
        map.put("fileList",files);
        map.put("treeList",treeList);
        log.debug("treeList:{}",treeList.toString());
        return map;

    }

    /**
     * 更新文件路径
     * @param file
     */
    @Transactional
    public void updateFilePath(File file) {
        fileMapper.updateById(file);
    }

    /**
     * 删除目标文件
     * @param memId
     * @param url
     */
    @Override
    @Transactional
    public void deleteFile(String memId, String url) {
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(File::getMemId,memId);
        queryWrapper.likeRight(File::getFDir,url);
        fileMapper.delete(queryWrapper);
    }


    /**
     * 获取与搜索相关的目录
     * @param treeNode
     * @param name
     * @param treeList
     */
    private static void findTreeNode(TreeNode treeNode, String name, List treeList) {
//        System.out.println(treeNode);
//        System.out.println(list1);
        List<TreeNode> list = treeNode.getChildrenList();
//        System.out.println(list);
        if (list==null || list.isEmpty()){
            return;
        }
        else {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getName().contains(name)){
                    //System.out.println(list.get(i).getChildrenList());
                   treeList.add(list.get(i));
                    List<TreeNode> listTemp =list.get(i).getChildrenList();
                    System.out.println(listTemp);
                    if (listTemp.size()>=1){
                        findTreeNode(list.get(i),name,treeList);
                    }
                }else{
                    findTreeNode(list.get(i),name,treeList);
                }
            }
        }

    }

    /**
     * @description: 同步文件至数据库
     * @author L J
     * @date 2023/8/28 12:03
     * @param file File
     * @return void
     */
    @Override
    @Transactional
    public File addDbFile(File file) {
        fileMapper.insert(file);
        return file;
    }

    /**
     * @description:更新文件
     * @author L J
     * @date 2023/8/30 13:24
     * @param id String 文件id
     * @param rename String 新名称
     * @return void
     */
    @Override
    @Transactional
    public void updateFile(String id, String rename) {
        File file = fileMapper.selectById(id);
        file.setName(rename);
        fileMapper.updateById(file);
    }

}
